/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package name_service;

import mware_lib.IBaseSkeleton;
import mware_lib.NameService;

/**
 * @author Gortex
 */
public class NameServiceSkeleton implements IBaseSkeleton {

    NameService original;

    protected NameServiceSkeleton(NameService original) {
        this.original = original;
    }

    @Override
    public String remoteInvoke(String message) {
        try {
            String[] messageAry = message.split(";");

            if (messageAry[0].split(":")[1].equals("rebind")) {

                String typ1 = messageAry[1].split(":")[1];
                String typ2 = messageAry[3].split(":")[1];

                if (typ1.equals("String") && typ2.equals("String")) {
                    int beginIndex = messageAry[2].indexOf(':');
                    String param1 = messageAry[2].substring(beginIndex + 1, messageAry[2].length());
                    String param2 = messageAry[4].split(":")[1];
                    String returnval = "exception:none;returntyp:void;returnvalue:void";
                    original.rebind(param1, param2);
                    return returnval;

                } else {
                    return "exception:exception;returntyp:RuntimeException;returnvalue:typemismatch";
                }

            } else if (messageAry[0].split(":")[1].equals("resolve")) {
                String typ1 = messageAry[1].split(":")[1];
                if (typ1.equals("String")) {
                    String param1 = messageAry[2].split(":")[1];
                    Object object = original.resolve(param1);
                    if(object == null){
                         return "exception:exception;returntyp:RuntimeException;returnvalue:reference not found";
                    }else{
                        return "exception:none;returntyp:String;returnvalue:" + original.resolve(param1);
                    }
                } else {
                    return "exception:exception;returntyp:RuntimeException;returnvalue:typemismatch";
                }
            } else {
                return "exception:exception;returntyp:RuntimeException;returnvalue:method not found";
            }
        } catch (Exception e) {
            return "exception:exception;returntyp:RuntimeException;returnvalue:" + e.getMessage();
        }
    }
}
